﻿using System.Collections.Generic;
using System.Linq;
using System;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Longtop.Web.Helpers {

    public static class ObjectExtentions {


        public static T AsModel<T>(this Excel.IExcelDataReader reader,T Model) {
            return (T)new object();
        }


        /// <summary>
        /// 格式化成Json字符串
        /// </summary>
        /// <param name="obj">需要格式化的对象</param>
        /// <returns>Json字符串</returns>
        public static string ToJson(this object obj) {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(obj);
        }

        /// <summary>
        /// 格式化成Json字符串
        /// </summary>
        /// <param name="obj">需要格式化的对象</param>
        /// <param name="recursionDepth">指定序列化的深度</param>
        /// <returns>Json字符串</returns>
        public static string ToJson(this object obj, int recursionDepth) {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            serializer.RecursionLimit = recursionDepth;
            return serializer.Serialize(obj);
        }

    }

    public static class HtmlExtensions {
        private static Action<T> Fix<T>(Func<Action<T>, Action<T>> f) {
            return x => f(Fix(f))(x);
        }

        public static void Render<T>(this HtmlHelper helper, T model, Func<Action<T>, Action<T>> f) {
            Fix(f)(model);
        }
    }



    [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
    public class AttachDataAttribute : Attribute {
        public AttachDataAttribute(object key, object value) {
            this.Key = key;
            this.Value = value;
        }

        public object Key { get; private set; }

        public object Value { get; private set; }
    }
    public static class AttachDataExtensions {
        public static object GetAttachedData(
            this ICustomAttributeProvider provider, object key) {
            var attributes = (AttachDataAttribute[])provider.GetCustomAttributes(
                typeof(AttachDataAttribute), false);
            return attributes.First(a => a.Key.Equals(key)).Value;
        }

        public static T GetAttachedData<T>(
            this ICustomAttributeProvider provider, object key) {
            return (T)provider.GetAttachedData(key);
        }

        public static object GetAttachedData(this Enum value, object key) {
            return value.GetType().GetField(value.ToString()).GetAttachedData(key);
        }

        public static T GetAttachedData<T>(this Enum value, object key) {
            return (T)value.GetAttachedData(key);
        }
    }
    public enum AgeRange {
        [AttachData(AgeRangeAttachData.Text, "18岁及以下")]
        LessThan18,

        [AttachData(AgeRangeAttachData.Text, "19至29岁")]
        From19To29,

        [AttachData(AgeRangeAttachData.Text, "30岁及以上")]
        Above29
    }
    public enum AgeRangeAttachData {
        Text
    }
    public static class AgeRangeExtensions {
        public static string GetText(this AgeRange range) {
            return range.GetAttachedData<string>(AgeRangeAttachData.Text);
        }
    }





}